﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonUtils;
using TERP.WaterClient.Common;
using TERP.WaterClient.DbServices;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmSumTotal : FrmMdiBase
    {
        #region Pro

        private const string Field_TotalAmount = "AMOUNT";
        private const string Field_AmountPaid = "AMOUNTPAID";
        private const string Field_AmountDebit = "AMOUNTDEBIT";

		//ITS.Library.WaterStockOutService.WaterStockOutServiceClient _clientStockOut;
		StockOutService _stockOutService;

        #endregion

        #region Method

        void InitControl()
        {
            CusName.FieldName = TblCustomer.CusName;
            StockOutCode.FieldName = TblStockOut.StockOutCode;
			StockOutDate.FieldName = TblStockOut.StockOutDate;
            AmountPaid.FieldName = TblStockOut.Note;
			Price.FieldName = TblStockOut.Price;
			PriceCuocVo.FieldName = TblStockOut.PriceCuocVo;
			NoVo.FieldName = TblStockOut.NoVo;
			Quantity.FieldName = TblStockOut.Quantity;
			CuocVo.FieldName = TblStockOut.CuocVo;
            TotalAmount.FieldName = Field_TotalAmount;
            AmountDebit.FieldName = Field_AmountDebit;
            AmountPaid.FieldName = Field_AmountPaid;

            gridView1.OptionsBehavior.Editable = false;

            this.SetGridViewIndexNormal(gridView1);
            //Format
            this.SetGridColumnDateFormat(StockOutDate);
        }

        void SearchData()
        {
            try
            {
				//_clientStockOut = new ITS.Library.WaterStockOutService.WaterStockOutServiceClient();
				//List<ITS.Library.WaterStockOutService.WaterStockOut> _lst = _clientStockOut.GetAll().ToList(); _clientStockOut.Close();

				//if (!StringUtils.IsEmpty(strCusName))
				//{
				//    _lst = _lst.Where(p => p.CusName.ToLower().Contains(strCusName.ToLower())).ToList();
				//}
				//_lst = _lst.Where(p => p.StockOutDate.Value.Date >= dtFromDate && p.StockOutDate.Value.Date <= dtToDate).ToList();

				//int i = 1;
				//var query = from p in _lst
				//            orderby p.StockOutDate descending
				//            select new
				//            {
				//                Index = i++,
				//                p.CuocVo,
				//                p.CusCode,
				//                p.CusName,
				//                p.Note,
				//                p.NoVo,
				//                p.Price,
				//                p.PriceCuocVo,
				//                p.Quantity,
				//                p.StockOutCode,
				//                p.StockOutDate,
				//                p.StockOutID,
				//                p.Unit,
				//                TotalAmount = p.Price * p.Quantity + p.PriceCuocVo * p.CuocVo,
				//                p.AmountPaid,
				//                AmountDebit=p.Price*p.Quantity + p.PriceCuocVo*p.CuocVo-p.AmountPaid
				//            };
				//gridControl1.DataSource = query.ToList();

				_stockOutService = new StockOutService();
				DataTable dt_StockOut = _stockOutService.Its_StockOut_Get(""
																		, this.GetDateEditStringValue(dedFromDate, Nonsense.DATE_DB_FORMAT)
																		, this.GetDateEditStringValue(dedToDate, Nonsense.DATE_DB_FORMAT)
																		, ""
																		, ""
																		, ""
																		, "").Tables[DbTable.Tbl_StockOut];

				gridControl1.DataSource = dt_StockOut;
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }
        #endregion

        #region Event
        public FrmSumTotal()
        {
            InitializeComponent();

            InitControl();

            dedFromDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            dedFromDate.Properties.DisplayFormat.FormatString = Nonsense.DATE_FORMAT;
            dedFromDate.Properties.EditMask = Nonsense.DATE_FORMAT;

            dedToDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            dedToDate.Properties.DisplayFormat.FormatString = Nonsense.DATE_FORMAT;
            dedToDate.Properties.EditMask = Nonsense.DATE_FORMAT;

            dedFromDate.DateTime = DateTime.Now;
            dedToDate.DateTime = DateTime.Now;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNhapMoi_Click(object sender, EventArgs e)
        {
            FrmCustomer frm = new FrmCustomer();
            frm.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            DateTime dtFromDate = dedFromDate.DateTime.Date;
            DateTime dtToDate = dedToDate.DateTime.Date;
            SearchData();
        }

        private void FrmWaterStockOutList_Load(object sender, EventArgs e)
        {
            DateTime dtFromDate = dedFromDate.DateTime.Date;
            DateTime dtToDate = dedToDate.DateTime.Date;

			SearchData();
        }
        #endregion
    }
}
